﻿//
// Asynchronous handlers of file system events
//

using System;
using System.IO;
using System.Diagnostics;

using FileTreeSpace;
using TravPresenterAbstraction;


namespace TravModelSpace
{
    public partial class TravModel
    {
        private void DirCreater (string newPathName, string newLeafName)
        {
            if (Presenter.InvokeRequired ())
            {
                DirCreateEventHandler cb = new DirCreateEventHandler (DirCreater);
                Presenter.Invoke (cb, new object[] { newPathName, newLeafName });
            }
            else
            {
                OnFolderCreated (newPathName, newLeafName);

                DirPath p = new DirPath (Tree, newPathName);

                for (int k = 0; k < p.Leaf.Files.Count; ++k)
                    Detail.OnFileAdded
                        (p.Leaf.Files[k].Name,
                        p.Leaf.Files[k].Size,
                        null,
                        newPathName,
                        p.Leaf.Files[k].DateModified,
                        p.Leaf.Files[k].Attributes);

                RefreshStatus ();
            }
        }


        private void DirRenamer (string oldPathName, string oldLeafName, string newLeafName)
        {
            if (Presenter.InvokeRequired ())
            {
                DirRenameEventHandler cb = new DirRenameEventHandler (DirRenamer);
                Presenter.Invoke (cb, new object[] { oldPathName, oldLeafName, newLeafName });
            }
            else
                OnFolderRenamed (oldPathName, oldLeafName, newLeafName);
        }


        private void DirDeleter (string oldPathName)
        {
            if (Presenter.InvokeRequired ())
            {
                DirDeleteEventHandler cb = new DirDeleteEventHandler (DirDeleter);
                Presenter.Invoke (cb, new object[] { oldPathName });
            }
            else
            {
                // Address bar will be auto updated when tree node is removed.
                OnFolderRemoved (oldPathName);
                RefreshStatus ();
            }
        }


        private void FileCreater (string newFullName, string newBaseName, long newSize, DateTime dateModified, FileAttributes newAttributes)
        {
            if (Presenter.InvokeRequired ())
            {
                FileCreateEventHandler cb = new FileCreateEventHandler (FileCreater);
                Presenter.Invoke (cb, new object[] { newFullName, newBaseName, newSize, dateModified, newAttributes });
            }
            else
            {
                int pathLen = newFullName.Length - newBaseName.Length - 1;
                if (pathLen < CurrentDir.Length)
                    return;

                if (pathLen == CurrentDir.Length || pathLen > CurrentDir.Length && newFullName[CurrentDir.Length] == '\\')
                    if (!newFullName.StartsWith (CurrentDir))
                        return;

#if DEBUG
                foreach (string fullName in Detail.Presenter.FullNames)
                    if (fullName == newFullName)
                    {
                        Debug.Fail ("FileCreater already has row");
                        return;
                    }
#endif

                Detail.OnFileAdded (newBaseName, newSize, null, newFullName.Substring (0, pathLen), dateModified, newAttributes);
                RefreshStatus ();
            }
        }


        private void FileChanger (string changedFullName, long newSize, FileAttributes newAttributes)
        {
            if (Presenter.InvokeRequired ())
            {
                FileChangeEventHandler cb = new FileChangeEventHandler (FileChanger);
                Presenter.Invoke (cb, new object[] { changedFullName, newSize, newAttributes });
            }
            else
            {
                Detail.OnFileChanged (changedFullName, newSize, newAttributes);
                RefreshStatus ();
            }
        }


        private void FileRenamer (string oldFullName, string newFullName, string newBaseName)
        {
            if (Presenter.InvokeRequired ())
            {
                FileRenameEventHandler cb = new FileRenameEventHandler (FileRenamer);
                Presenter.Invoke (cb, new object[] { oldFullName, newFullName, newBaseName });
            }
            else
                Detail.OnFileRenamed (oldFullName, newFullName, newBaseName);
        }


        private void FileDeleter (string deletedFullName)
        {
            if (Presenter.InvokeRequired ())
            {
                FileDeleteEventHandler cb = new FileDeleteEventHandler (FileDeleter);
                Presenter.Invoke (cb, new object[] { deletedFullName });
            }
            else
            {
                Detail.OnFileRemoved (deletedFullName);
                RefreshStatus ();
            }
        }

    }
}
